function Test-Endpoint {
    param (
        [string]$Uri,
        [string]$Method = "GET",
        $Body = $null
    )

    Write-Host "\n=== 测试 $Uri ($Method) ==="
    try {
        $params = @{
            Uri = $Uri
            Method = $Method
            UseBasicParsing = $true
            ErrorAction = "Stop"
        }

        if ($Body -ne $null) {
            $params.Add("Body", ($Body | ConvertTo-Json))
            $params.Add("ContentType", "application/json")
        }

        $response = Invoke-WebRequest @params
        Write-Host "✅ 状态码: $($response.StatusCode)"
        Write-Host "响应内容: $($response.Content)"
        return $true
    } catch {
        Write-Host "❌ 失败: $($_.Exception.Message)"
        if ($_.Exception.Response) {
            $stream = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
            $content = $stream.ReadToEnd()
            Write-Host "错误内容: $content"
        }
        return $false
    }
}

# 测试各API端点
Test-Endpoint -Uri "http://localhost:5269/WeatherForecast"  # 测试基本功能
Test-Endpoint -Uri "http://localhost:5269/api/file/types"   # 测试获取文件类型
Test-Endpoint -Uri "http://localhost:5269/api/file"          # 测试获取文件列表

# 测试添加文件类型（重要）- 此端点已在服务器上实现
Write-Host "\n=== 测试添加文件类型（已实现）==="
$randomId = Get-Random -Minimum 1000 -Maximum 9999
$fileTypeBody = @{ TypeName = "测试类型$randomId"; MemoryStrength = 1.2 }
$addTypeResult = Test-Endpoint -Uri "http://localhost:5269/api/file/types" -Method "POST" -Body $fileTypeBody

# 如果添加成功，验证是否真的添加到系统中
if ($addTypeResult) {
    Write-Host "\n=== 验证新添加的文件类型 ==="
    # 再次获取所有文件类型，确认新类型是否存在
    Test-Endpoint -Uri "http://localhost:5269/api/file/types"  # 应该包含新添加的类型
}

# 查看测试结果总结
Write-Host "\n=== 测试完成 ==="